package org.example.service.impl;

import org.example.dao.MajorDao;
import org.example.dto.MajorDto;
import org.example.pojo.Major;
import org.example.service.MajorService;
import org.example.vo.MajorVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MajorServiceImpl implements MajorService {

    @Resource
    private MajorDao dao;

    private MajorVo PoTransToVo(Major major){
        MajorVo majorVo=new MajorVo();
        majorVo.setMajorId(major.getMajorno());
        majorVo.setMajorName(major.getMajorname());
        majorVo.setDepartment(major.getDepartname());
        return majorVo;
    }

    private MajorDto PoTransToDto(Major major){
        MajorDto majorDto=new MajorDto();
        majorDto.setMajorId(major.getMajorno());
        majorDto.setMajorName(major.getMajorname());
        majorDto.setDepartment(major.getDepartname());
        return majorDto;
    }
    private Major DtoTransToPo(MajorDto majorDto){
        Major major=new Major();
        major.setMajorno(majorDto.getMajorId());
        major.setMajorname(majorDto.getMajorName());
        major.setDepartname(majorDto.getDepartment());
        return major;
    }
    private Major VoTransToPo(MajorVo majorVo){
        Major major=new Major();
        major.setMajorno(majorVo.getMajorId());
        major.setMajorname(majorVo.getMajorName());
        major.setDepartname(majorVo.getDepartment());
        return major;
    }

    @Override
    public Integer addMajor(MajorVo major) {
        if(dao.insert(VoTransToPo(major)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer updateMajor(MajorVo major) {
        if(dao.update(VoTransToPo(major)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer deleteMajor(Integer majorId) {
        if(dao.delete(majorId))
            return 1;
        else
            return 0;
    }

    @Override
    public List<MajorDto> queryAllMajor() {
        List<Major> majorList=dao.queryAll();
        List<MajorDto> majorDtoList=new ArrayList<>();
        for(int i=0;i<majorList.size();i++)
            majorDtoList.add(i,PoTransToDto(majorList.get(i)));
        return majorDtoList;
    }

    @Override
    public Integer countMajor() {
        return dao.queryAll().size();
    }

    @Override
    public List<MajorDto> queryMajorByDepartmentName(String departmentName) {
        Major major=new Major();
        major.setDepartname(departmentName);
        List<Major> majorList=dao.queryByCondition(major);
        List<MajorDto> majorDtoList=new ArrayList<>();
        for(int i=0;i<majorList.size();i++)
            majorDtoList.add(i,PoTransToDto(majorList.get(i)));
        return majorDtoList;
    }

    @Override
    public MajorDto queryMajorById(Integer majorId) {
        Major major=new Major();
        major.setMajorno(majorId);
        return PoTransToDto(dao.queryByCondition(major).get(0));
    }

    @Override
    public Integer countMajorByDepartment(String department) {
        Major major=new Major();
        major.setDepartname(department);
        return dao.queryByCondition(major).size();
    }

}
